package com.llkj.contractmanagement.contractmanagement.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.llkj.contractmanagement.main.BaseFragment;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.contractmanagement.actor.ActorDetailsActivity;
import com.llkj.contractmanagement.contractmanagement.adapter.BrowsingHstoryActorAdapter;
import com.llkj.contractmanagement.http.HttpMethod;
import com.llkj.contractmanagement.http.UrlConfig;
import com.llkj.contractmanagement.main.MyApplication;
import com.llkj.contractmanagement.model.ActorBean;
import com.llkj.contractmanagement.model.UserInfoBean;
import com.llkj.contractmanagement.utils.GsonUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * author: dong.yj
 * created on: 2016/8/18 14:40
 * description:浏览足迹-艺人
 */
public class BrowsingHistoryActorFragment extends BaseFragment implements AdapterView.OnItemClickListener, BGARefreshLayout.BGARefreshLayoutDelegate {
    @Override
    public int getContentViewLayout() {
        return R.layout.fragment_browsinghistory_actor;
    }

    @Bind(R.id.bga_refresh)
    BGARefreshLayout mRefreshLayout;
    @Bind(R.id.lv_browsinghistory_actor)
    ListView lv_actor;
    private BrowsingHstoryActorAdapter adapter;
    private List<ActorBean.YrlistBean> actorList;
    private UserInfoBean userInfoBean;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    private void init() {
        initData();
        setListener();
    }

    private void initData() {
        mRefreshLayout.setRefreshViewHolder(new BGANormalRefreshViewHolder(MyApplication.getInstance(), true));
        userInfoBean = UserInfoBean.getInstance();
        actorList = new ArrayList<>();
        adapter = new BrowsingHstoryActorAdapter(ctx, actorList);
        lv_actor.setAdapter(adapter);
        requestBrowsingHistoryActor();
    }

    private void requestBrowsingHistoryActor() {
        HashMap<String, String> map = new HashMap<>();
//        user_id	1	string	用户的id
//        token	1	string	Token
        map.put("user_id", userInfoBean.getId());
        map.put("token", userInfoBean.getToken());
        HttpMethod.zjyr(this, ctx, map, UrlConfig.HTTP_ZJ_YR);
    }

    private void setListener() {
        lv_actor.setOnItemClickListener(this);
        mRefreshLayout.setDelegate(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(ctx, ActorDetailsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("YrList", actorList.get(position));
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void onSuccessHttp(String responseInfo, int resultCode) {
        super.onSuccessHttp(responseInfo, resultCode);
        switch (resultCode) {
            case UrlConfig.HTTP_ZJ_YR://浏览足迹艺人
                mRefreshLayout.endRefreshing();
                ActorBean actorBean = GsonUtil.GsonToBean(responseInfo, ActorBean.class);
                if (actorBean != null) {
                    List<ActorBean.YrlistBean> addList = actorBean.getYrlist();
                    if (addList != null && addList.size() > 0) {
                        actorList.addAll(addList);
                        adapter.notifyDataSetChanged();
                    }
                }
                break;
            case UrlConfig.HTTP_DEL_YRZJ:
                actorList.clear();
                adapter.notifyDataSetChanged();
                break;
        }
    }

    public void cleanDate() {
        HashMap<String, String> map = new HashMap<>();
//        user_id	1	string	用户的id
//        token	1	string	Token
        map.put("user_id",userInfoBean.getId());
        map.put("token", userInfoBean.getToken());
        HttpMethod.delYrzj(this, ctx, map, UrlConfig.HTTP_DEL_YRZJ);
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        actorList.clear();
        requestBrowsingHistoryActor();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }
}
